package com.amigo.online.provider.sync.convert.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.entity.VideoMysqlInfo;
import com.amigo.online.provider.compare.mapper.UserComparisonMapper;
import com.amigo.online.provider.sync.abs.BasicMongoDataProjectProperties;
import com.amigo.online.provider.sync.convert.AbstractColumnConvertUtilService;
import com.amigo.online.provider.sync.convert.ConvertJsonObjectService;
import com.amigo.online.provider.sync.convert.model.VideoUserBO;

/**
 * @version: V 1.0.0
 * @Description: （MYSQL 和 Mongo 对应video对象实体转换）
 * @author: zxy QQ:1363320658
 * @date: 2018年12月10日 下午4:15:16
 */
@Service("mongoVideoConvertServiceImpl")
public class MongoVideoConvertServiceImpl extends AbstractColumnConvertUtilService
        implements ConvertJsonObjectService<Document, JSONObject, VideoMysqlInfo> {

    @Autowired
    BasicMongoDataProjectProperties basicMongoDataProjectProperties;

    @Autowired
    UserComparisonMapper userComparisonMapper;

    @Override
    public Document convert(JSONObject source) {
        Document videoDoc = new Document();
        Map<String, String> projectMap = basicMongoDataProjectProperties.getVideoProjectMap();
        for (String temp : basicMongoDataProjectProperties.getVideoUpdateList()) {
            String projectColumn = projectMap.get(temp);
            if (projectColumn != null) {
                videoDoc.append(projectColumn, source.get(temp));
            } else {
                videoDoc.append(temp, source.get(temp));
            }
        }
        List<Double> point = new ArrayList<Double>();
        String longitude = source.getString("longitude");
        String lantitude = source.getString("latitude");
        if (longitude != null) {
            point.add(Double.parseDouble(longitude));
        } else {
            point.add(0d);
        }
        if (lantitude != null) {
            point.add(Double.parseDouble(longitude));
        } else {
            point.add(0d);
        }
        Document loc = new Document();
        loc.append("type", "Point").append("coordinates", point);
        VideoUserBO videoUserBO = userComparisonMapper.findVideoUserInfoByUserId(source.getLongValue("user_id"));
        if (videoUserBO == null) {
            videoUserBO = new VideoUserBO();
        }

        videoDoc.append("loc", loc).append("nickname", videoUserBO.getNickname())
                .append("avatar", videoUserBO.getAvatar())
                .append("_class", "com.amigo.online.provider.video.mongo.entity.VideoEntity");
        /*
         * videoDoc.append("_id", source.getLongValue("id")) .append("user_id",
         * source.getLongValue("user_id")) .append("cover", source.getString("cover"))
         * .append("recommended", source.getIntValue("recommended")) .append("nickname",
         * source.getString("nickname")) .append("avatar", source.getString("avatar"))
         * .append("type", source.getInteger("type")) .append("created_time",
         * source.getLongValue("created_time")) .append("loc", loc) .append("_class",
         * "com.amigo.online.provider.video.mongo.entity.VideoEntity");
         */
        return videoDoc;
    }

    @Override
    public String convertColumn(String source) {
        String result = source;
        /*
         * switch(source) { case "id" : result = "_id"; break; case "user_id" : result =
         * "userId"; break; case "created_time" : result = "createTime"; break; }
         */
        return result;
    }

    @Override
    public Document convertBeanToObject(VideoMysqlInfo source) {
        Document videoDoc = new Document();
        List<Double> point = new ArrayList<Double>();
        point.add(0d);
        point.add(0d);
        Document loc = new Document();
        VideoUserBO videoUserBO = userComparisonMapper.findVideoUserInfoByUserId(source.getUserId());
        loc.append("type", "Point").append("coordinates", point);
        videoDoc.append("loc", loc).append("nickname", videoUserBO.getNickname())
                .append("avatar", videoUserBO.getAvatar())
                .append("_class", "com.amigo.online.provider.video.mongo.entity.VideoEntity");
        videoDoc.append("_id", source.getId()).append("userId", source.getUserId()).append("cover", source.getCover())
                .append("recommended", source.getRecommended()).append("nickname", source.getNickname())
                .append("avatar", source.getAvatar()).append("type", source.getType())
                .append("createdTime", source.getCreateTime()).append("loc", loc)
                .append("_class", "com.amigo.online.provider.video.mongo.entity.VideoEntity");
        return videoDoc;
    }

}
